<?php
namespace app\task\controller;
use app\common\library\wechat\WxBizMsgCrypt;
use app\task\model\Config as ConfigModel;
//use app\task\model\Wxapp as WxappModel;
use app\task\model\WebSet as WebSetModel;
use app\task\model\Keyword as KeywordModel;
//use app\task\model\User as UserModel;
use app\task\model\StoreUser;
//use app\task\model\NearbyCategory as NearbyCategoryModel;
//use app\task\model\Nearby as NearbyModel;
/**
 * 第三方平台与微信通讯接口
 */
class Wechat
{
    /**
     * 异步通知处理
     */
    public function callback($appid='')
    {
		// 接收公众号平台发送的消息
		$nonce = empty ( $_GET ['nonce'] ) ?"" : trim ( $_GET ['nonce'] );
		$signature = empty ( $_GET['signature'] ) ? "" : trim ( $_GET ['signature'] );
		$timeStamp = empty ( $_GET ['timestamp']) ? "" : trim ( $_GET ['timestamp'] );
		$msg_signature = empty ( $_GET['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] );
		$encryptMsg = file_get_contents ('php://input' );
		//write_log($appid, __DIR__);//公众号的APPID
		//获取第三方配置信息
		$config = ConfigModel::detail();
        //创建解密类
		$pc = new WxBizMsgCrypt($config['token'], $config['encoding_aes_key'], $config['app_id']);
		$msg = '';		
		$errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg);	
		if($errCode == 0){
			$data = _xmlToArr($msg);	//XML转换为数组
			//write_log($data, __DIR__);
			
			//*********************************接收事件推送*****************************************
			if($data['MsgType']=='event'){
				//用户未关注时，进行关注后的事件推送
				if($data['Event']=='subscribe'){
					//是否设置了关注回复
					if($subscribe = WebSetModel::getItem('subscribe')){
						$this->replyMsg($subscribe,$data);//回复信息
					} 
					//获取粉丝资料
					if($user = getWechatUserInfo($data['FromUserName'])){
						//查询是否为历史用户
						if($store = StoreUser::getStore(['union_id' => $user['union_id']])){
							//是，更新资料
							$store->edit($user);
						}else{
							//否，新增
							$store = new StoreUser;
							$store->add($user);
						}
					}
					return '';//阻止重复推送
				}
				
				//用户已关注时扫码进入事件推送
				if($data['Event']=='SCAN'){
					
				}
				//取消关注事件
				if($data['Event']=='unsubscribe'){
					if($store = StoreUser::getStore(['wechat_open_id' => $data['FromUserName']])){
						$store->unSubscribe();
					}
					return '';//阻止重复推送
				}
				
				//扫描带参数二维码事件 - 用户未关注时，进行关注后的事件推送
				if($data['Event']=='subscribe' AND isset($data['EventKey'])){
		
				}
				
				//扫描带参数二维码事件 - 用户已关注时的事件推送
				if($data['Event']=='SCAN'){
				
				}
				
				//上报地理位置事件
				if($data['Event']=='LOCATION'){
					
				}
				
				//自定义菜单事件 - 点击菜单拉取消息时的事件推送
				if($data['Event']=='CLICK'){
					
				}
				
				//自定义菜单事件 - 点击菜单跳转链接时的事件推送
				if($data['Event']=='VIEW'){
					
				}
				//添加附近门店类目审核回调
				if($data['Event']=='nearby_category_audit_info'){
					//$this->nearby_category_audit_info($data);
				}
				
				//添加附近小程序地点审核回调
				if($data['Event']=='add_nearby_poi_audit_info'){
					write_log('添加附近小程序地点审核回调',__DIR__);
					write_log($data,__DIR__);
					//$this->add_nearby_poi_audit_info($data);
				}
			}
			//*********************************接收普通消息*****************************************
			//接收普通消息 - 文本消息
			if($data['MsgType']=='text'){
				//获取平台保留关键字
				$keyword = WebSetModel::getItem('register');
				$prefix = empty($keyword['prefix'])?"hema" : trim ($keyword['prefix']);//账号前缀
				$register = empty($keyword['register'])?"注册" : trim ($keyword['register']);//注册命令
				$username = empty($keyword['username'])?"找回账号" : trim ($keyword['username']);//找回账号命令
				$password = empty($keyword['password'])?"找回密码" : trim ($keyword['password']);//找回密码命令
				//是否设置了关键字回复
				switch($data['Content']){
					case $register:
						//获取用户信息
						$user = StoreUser::getStore(['wechat_open_id' => $data['FromUserName']]);
						//判断是否注册商户账号
						if(empty($user['user_name'])){
							//如果不存在，自动注册
							$pwd = rand(1000,9999);//随机生成四位数的密码
							$user->user_name = $prefix . $user['store_user_id'];
							$user->password = hema_hash($pwd);
							if($user->save()){
								//返回文本提醒
								echo $this->msgTpl([
									'ToUserName' => $data['FromUserName'],
									'FromUserName' => $data['ToUserName'],
									'MsgType' => 'text',
									'Content' => '恭喜：注册成功~
账号：' . $user->user_name .'
密码：' . $pwd . '
如忘记密码,请回复 “'. $password .'”'
								]);
							}else{
								//返回文本提醒
								echo $this->msgTpl([
									'ToUserName' => $data['FromUserName'],
									'FromUserName' => $data['ToUserName'],
									'MsgType' => 'text',
									'Content' => '注册失败~'
								]);
							}
						}else{
							//返回文本提醒
							echo $this->msgTpl([
								'ToUserName' => $data['FromUserName'],
								'FromUserName' => $data['ToUserName'],
								'MsgType' => 'text',
								'Content' => '您已经注册过了~
账号：'.$user->user_name .'
如忘记密码,请回复 “'. $password .'”'
							]);
						}
					break;
					case $password:
						//获取用户信息ID
						$user = StoreUser::getStore(['wechat_open_id' => $data['FromUserName']]);
						//判断是否注册商户账号
						if(empty($user['user_name'])){
							//返回文本提醒
							echo $this->msgTpl([
								'ToUserName' => $data['FromUserName'],
								'FromUserName' => $data['ToUserName'],
								'MsgType' => 'text',
								'Content' => '您还没有注册过账号~
请回复 “'. $register .'”，完成注册。'
							]);
						}else{
							$pwd = rand(1000,9999);//随机生成四位数的密码
							$user->password = hema_hash($pwd);
							$user->save();
							//返回文本提醒
								echo $this->msgTpl([
									'ToUserName' => $data['FromUserName'],
									'FromUserName' => $data['ToUserName'],
									'MsgType' => 'text',
									'Content' => '恭喜：成功找回密码~
您的新密码：'.$pwd
								]);
						}
					break;
					case $username:
						//获取用户信息ID
						$user = StoreUser::getStore(['wechat_open_id' => $data['FromUserName']]);
						//判断是否注册商户账号
						if(empty($user['user_name'])){
							//返回文本提醒
							echo $this->msgTpl([
								'ToUserName' => $data['FromUserName'],
								'FromUserName' => $data['ToUserName'],
								'MsgType' => 'text',
								'Content' => '您还没有注册过账号~
请回复 “'. $register .'”，完成注册。'
							]);
						}else{
							//返回文本提醒
								echo $this->msgTpl([
									'ToUserName' => $data['FromUserName'],
									'FromUserName' => $data['ToUserName'],
									'MsgType' => 'text',
									'Content' => '恭喜：成功找回账号~
您的账号：'.$user['user_name']
								]);
						}
					break;
					default:
						if($values = KeywordModel::getKey($data['Content'])){
							$this->replyMsg($values,$data);//回复信息
						}else{
							//没有，发什么回什么
							echo $this->msgTpl([
								'ToUserName' => $data['FromUserName'],
								'FromUserName' => $data['ToUserName'],
								'MsgType' => $data['MsgType'],
								'Content' => $data['Content']
							]);
						}
				}
			}
			
			//接收普通消息 - 图片消息
			if($data['MsgType']=='image'){
				//发什么回什么
				echo $this->msgTpl([
					'ToUserName' => $data['FromUserName'],
					'FromUserName' => $data['ToUserName'],
					'MsgType' => $data['MsgType'],
					'MediaId' => $data['MediaId']
				]);
			}
			
			//接收普通消息 - 语音消息
			if($data['MsgType']=='voice'){
				//发什么回什么
				echo $this->msgTpl([
					'ToUserName' => $data['FromUserName'],
					'FromUserName' => $data['ToUserName'],
					'MsgType' => $data['MsgType'],
					'MediaId' => $data['MediaId']
				]);
			}
			
			//接收普通消息 - 视频消息
			if($data['MsgType']=='video'){
				//发什么回什么
				echo $this->msgTpl([
					'ToUserName' => $data['FromUserName'],
					'FromUserName' => $data['ToUserName'],
					'MsgType' => 'text',
					'Content' => '我不看，太低俗~'
				]);
			}
			
			//接收普通消息 - 小视频消息
			if($data['MsgType']=='shortvideo'){
				
			}
			
			//接收普通消息 - 地理位置消息
			if($data['MsgType']=='location'){
				
			}
			
			//接收普通消息 - 链接消息
			if($data['MsgType']=='link'){
				
			}
			return 'success';
		}else{
			write_log('Wechat 解密失败 - 错误代码：'.$errCode, __DIR__);
		}
    }
	
	/**
     * 附近门店类目回调处理
    
	private function nearby_category_audit_info($data)
	{
		$model = NearbyCategoryModel::getDetail(['audit_id' => $data['audit_id']]);
		$model->edit($data);
	}
	*/
	/**
     * 添加附近展示地点回调处理
    
	private function add_nearby_poi_audit_info($data)
	{
		$model = NearbyModel::getDetail(['audit_id' => $data['audit_id']]);
		$model->edit($data);
	}
	*/
	/**
     * 将array转为xml
    */
	private function msgTpl($arr)
	{
		$xml ="<xml><ToUserName><![CDATA[" . $arr['ToUserName'] . "]]></ToUserName>";
		$xml .="<FromUserName><![CDATA[" . $arr['FromUserName'] . "]]></FromUserName>";
		$xml .="<CreateTime>" . time() . "</CreateTime>";
		$xml .="<MsgType><![CDATA[" . $arr['MsgType'] . "]]></MsgType>";
		switch($arr['MsgType']){
			case "text":
				$xml .="<Content><![CDATA[" . $arr['Content'] . "]]></Content>";
				break;
			case "image":
				$xml .= "<Image><MediaId><![CDATA[" . $arr['MediaId'] . "]]></MediaId></Image>";
				break;
			case "voice":
				$xml .= "<Voice><MediaId><![CDATA[" . $arr['MediaId'] . "]]></MediaId></Voice>";
				break;
			case "video":
				$xml .= "<Video><MediaId><![CDATA[" . $arr['MediaId'] . "]]></MediaId><Title><![CDATA[" . $arr['Title'] . "]]></Title><Description><![CDATA[" . $arr['Description'] . "]]></Description></Video>";
				break;
			case "music":
				$xml .= "<Music><Title><![CDATA[" . $arr['Title'] . "]]></Title><Description><![CDATA[" . $arr['Description'] . "]]></Description><MusicUrl><![CDATA[" . $arr['MusicUrl'] . "]]></MusicUrl><HQMusicUrl><![CDATA[" . $arr['HQMusicUrl'] . "]]></HQMusicUrl><ThumbMediaId><![CDATA[" . $arr['ThumbMediaId'] . "]]></ThumbMediaId></Music>";
				break;
			case "news":
				$xml .= "<ArticleCount>". sizeof($arr['Articles']) ."</ArticleCount><Articles>";
				for($n=0;$n<sizeof($arr['Articles']);$n++){
					$xml .= "<item><Title><![CDATA[". $arr['Articles'][$n]['title'] ."]]></Title><Description><![CDATA[". $arr['Articles'][$n]['description'] ."]]></Description><PicUrl><![CDATA[". $arr['Articles'][$n]['picurl'] ."]]></PicUrl><Url><![CDATA[". $arr['Articles'][$n]['url'] ."]]></Url></item>";
				}
				$xml .= "</Articles>";
				break;
		}
		$xml .= "</xml>";
		return $xml;
	}
	
	/**
     * 回复消息
    */
	private function replyMsg($msg,$data)
	{
		//是否开启
		if($msg['is_open']==1){
			//文字消息
			if($msg['type']=='text'){
				echo $this->msgTpl([
					'ToUserName' => $data['FromUserName'],
					'FromUserName' => $data['ToUserName'],
					'MsgType' => 'text',
					'Content' => $msg['dataGroup']['text']['content']
				]);
			}			
			//图片消息
			if($msg['type']=='image'){
				echo $this->msgTpl([
				  'ToUserName' => $data['FromUserName'],
				  'FromUserName' => $data['ToUserName'],
				  'MsgType' => 'image',
				  'MediaId' => $msg['dataGroup']['image']['media_id']
				]);
			}
			//语音消息
			if($msg['type']=='voice'){
				echo $this->msgTpl([
				  'ToUserName' => $data['FromUserName'],
				  'FromUserName' => $data['ToUserName'],
				  'MsgType' => 'voice',
				  'MediaId' => $msg['dataGroup']['voice']['media_id']
				]);
			}
			//视频消息
			if($msg['type']=='video'){
				echo $this->msgTpl([
				  'ToUserName' => $data['FromUserName'],
				  'FromUserName' => $data['ToUserName'],
				  'MsgType' => 'video',
				  'Title' => $msg['dataGroup']['video']['title'],
				  'Description' => $msg['dataGroup']['video']['description'],
				  'MediaId' => $msg['dataGroup']['video']['media_id']
				]);	
			}
			//音乐消息
			if($msg['type']=='music'){
				echo $this->msgTpl([
				  'ToUserName' => $data['FromUserName'],
				  'FromUserName' => $data['ToUserName'],
				  'MsgType' => 'music',
				  'Title' => $msg['dataGroup']['music']['title'],
				  'Description' => $msg['dataGroup']['music']['description'],
				  'MusicUrl' => $msg['dataGroup']['music']['url'],
				  'HQMusicUrl' => $msg['dataGroup']['music']['hurl'],
				  'ThumbMediaId' => $msg['dataGroup']['music']['media_id']
				]);
			}
			//图文消息
			if($msg['type']=='news'){
				echo $this->msgTpl([
				  'ToUserName' => $data['FromUserName'],
				  'FromUserName' => $data['ToUserName'],
				  'MsgType' => 'news',
				  'Articles' => $msg['dataGroup']['news']['item']
				]);
			}
		}
	}	
}
